package com.huabo.contract.mysql.mapper;

import com.hbfk.util.PageInfo;
import com.huabo.contract.mysql.entity.TblContractProjectMySql;
import com.huabo.contract.mysql.vo.TblContractProjectVo;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.BaseMapper;

import java.util.List;

@Repository
public interface TblContractProjectMySqlMapper extends BaseMapper<TblContractProjectMySql> {

    @Select("SELECT * from TBL_CONTRACT_PROJECT WHERE PROJECTID= #{projectid} ")
    TblContractProjectMySql getById(String projectid);

    @SelectProvider(method = "selectCountByPageInfo", type = TblContractProjectMySqlMapperSqlConfig.class)
    Integer selectCountByPageInfo(PageInfo<TblContractProjectMySql> pageInfo, TblContractProjectVo tblContractProjectVo, Integer staffid) throws Exception;

    @Select("SELECT TNA.*,STAFF.REALNAME,ORG.ORGNAME "
            + "FROM TBL_CONTRACT_PROJECT TNA "
            + "LEFT JOIN TBL_STAFF STAFF ON STAFF.STAFFID = TNA.UNDERTAKESTAFFID "
            + "LEFT JOIN TBL_ORGANIZATION ORG ON ORG.ORGID = TNA.UNDERTAKEORGID "
            + " WHERE TNA.PROJECTID = #{projectid}")
    @Results({
            @Result(column = "PROJECTID", property = "projectid"),
            @Result(column = "PROJECTNAME", property = "projectname"),
            @Result(column = "PROJECTCODE", property = "projectcode"),
            @Result(column = "MEMO", property = "memo"),

            @Result(column = "REALNAME", property = "undertakestaff.realname"),
            @Result(column = "ORGNAME", property = "undertakeorg.orgname"),
    })
    TblContractProjectMySql selectById(@Param("projectid") Integer projectid) throws Exception;

    @SelectProvider(method = "selectListByPageInfo", type = TblContractProjectMySqlMapperSqlConfig.class)
    @Results({
            @Result(column = "PROJECTID", property = "projectid"),
            @Result(column = "PROJECTNAME", property = "projectname"),
            @Result(column = "PROJECTCODE", property = "projectcode"),
            @Result(column = "MEMO", property = "memo"),

            @Result(column = "REALNAME", property = "undertakestaff.realname"),
            @Result(column = "ORGNAME", property = "undertakeorg.orgname"),
    })
    List<TblContractProjectMySql> selectListByPageInfo(PageInfo<TblContractProjectMySql> pageInfo, TblContractProjectVo tblContractProjectVo, Integer staffid) throws Exception;

    @Delete("DELETE FROM TBL_CONTRACT_PROJECT WHERE PROJECTID = #{projectid}")
    void deleteById(Integer projectid) throws Exception;

    @SelectProvider(method = "selectPlanCodeByOrgid", type = TblContractProjectMySqlMapperSqlConfig.class)
    Integer selectPlanCodeByOrgid(TblContractProjectMySql plan) throws Exception;

    @InsertProvider(method = "insertEntity", type = TblContractProjectMySqlMapperSqlConfig.class)
    @Options(useGeneratedKeys = true, keyProperty = "projectid", keyColumn = "PROJECTID")
    void insertEntity(TblContractProjectMySql plan) throws Exception;

    @UpdateProvider(method = "updateEntity", type = TblContractProjectMySqlMapperSqlConfig.class)
    void updateEntity(TblContractProjectMySql plan) throws Exception;
}
